package cn.com.bluemoon.daps.model.core.upload;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author Jarod.Kong
 */
@Data
public class TableInfo {
    private Table table;
    private List<FieldInfo> fieldInfos = new ArrayList<>();

    @AllArgsConstructor
    @Getter
    public static class FieldInfo implements Position{

        private final Integer rowIndex;
        private final Cell comment, name, type, primary, nullable, defaultValue, desc;
    }

    @AllArgsConstructor
    @Getter
    public static class Table implements Position{

        private final Integer rowIndex;
        private final Cell name;
        private final Cell comment;
        private final Cell desc;
        private final Cell topicCategory;


    }

    public interface Position{
        default Integer getRowIndex() {
            return 0;
        }
        default Integer getColumnIndex() {
            return 0;
        }
    }

    @AllArgsConstructor
    @Getter
    public static class Row implements Position{
        // 1 表属性数据行 2 字段属性数据行 3 表属性行 4 字段属性行 -1 其他
        private final Integer type;

        private final Integer rowIndex;

        private final List<Cell> cells;
    }

    @Data
    @ToString
    public static class Cell implements Position{

        private Integer columnIndex;

        private String value;

        private String virtualValue;

        public String getValueOrDefault(String defaultVal) {
            return value == null ? defaultVal : value;
        }

        public Cell() {
        }

        public Cell(Integer columnIndex, String value) {
            this.columnIndex = columnIndex;
            this.value = value;
        }

        public Cell(Integer columnIndex, String value, String virtualValue) {
            this.columnIndex = columnIndex;
            this.value = value;
            this.virtualValue = virtualValue;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Cell cell = (Cell) o;
            return Objects.equals(columnIndex, cell.columnIndex) &&
                    Objects.equals(value, cell.value) &&
                    Objects.equals(virtualValue, cell.virtualValue);
        }

        @Override
        public int hashCode() {
            return Objects.hash(columnIndex, value, virtualValue);
        }
    }
}
